import { apiPrefix } from '../mina.config'

const request = (url, method = 'GET', data) => {
  return new Promise((resolve, reject) => {
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      url: apiPrefix + url,
      method, data,
      success(res) {
        // step1 接口格式校验
        const keys = {}
        Object.keys(res.data).map(i => {
          keys[i] = 1
        })
        if (!(keys.status && keys.code && keys.msg)) {
          reject({
            apiStatus: 'FAILURE',
            apiMes: '系统繁忙，请稍后再试'
          })
        }

        // step2
        let data = null
        switch (res.statusCode) {
          case 200:
            if (res.data && res.data.status) {
              data = {
                apiStatus: 'SUCCESS',
                apiCode: res.statusCode,
                apiMes: res.data.msg,
                data: res.data.data
              }
              resolve(data)
            } else {
              data = {
                apiStatus: 'FAILURE',
                apiCode: res.statusCode,
                apiMes: res.data.msg,
                data: res.data.data,
              }
              reject(data)
            }
            break;
          case 404:
          case 500:
            data = {
              apiStatus: 'FAILURE',
              apiCode: res.statusCode,
              data: res.data
            }
            reject(data)
            break;
          default:
            break;
        }
      },
      faile(res) {
        reject(res)
      }
    })
  })
}

export const apiLogin = data => request('/home/user/login', 'POST', data)

export const apiLogout = data => request('/home/api/logout', 'POST', data)

export const apiResetPassword = data => request('/home/api/reset', 'POST', data)

export const apiGetOpenidByCode = data => request('/home/api/getopenidbycode', 'POST', data)

export const apiNowOpenidInfo = data => request('/home/api/nowOpenidInfo', 'POST', data)

export const apiAuditProjectList = data => request('/Home/Audit/projectList', 'POST', data)

export const apiAuditSendCode = data => request('/Home/Audit/sendCode', 'POST', data)

export const apiAuditUpdatePassword = data => request('/Home/Audit/updPassword', 'POST', data)

export const apiAuditUpdatePhone = data => request('/Home/Audit/updPhone', 'POST', data)

export const apiAuditCreateActivity = data => request('/Home/Audit/add', 'POST', data)

export const apiAuditStoreList = data => request('/Home/Audit/getStoreInfo', 'POST', data)

export const apiUploadToken = data => request('/Home/Audit/uploadInfo', 'POST', data)

// 
export const apiActivityLine = data => request('/home/api/orderInfo', 'POST', data)

// 审核员活动列表
export const apiAuditActivityList = data => request('/Home/Audit/activityList', 'GET', data)

// 店长活动列表
export const apiStoremanagerActivityList = data => request('/Home/Storemanager/activityList', 'GET', data)

export const apiUserAddFeedback = data => request('/Home/User/addFeedBack', 'POST', data)

export const apiAuditUploadImage = data => request('/Home/Audit/upImg', 'POST', data)

// 订单详情
export const apiOrderDetailByOrderNo = data => request('/home/Order/detailByOrderNo', 'POST', data)

export const apiMaterial = data => request('/Home/Storemanager/materielList', 'POST', data)

export const apiActivityImageList = data => request('/Home/Storemanager/imgList', 'POST', data)

export const apiActivityDetail = data => request('/Home/Storemanager/activityDetail', 'GET', data)

export const apiMakeOrder = data => request('/Home/Storemanager/order', 'POST', data)


// 高空审核
export const apiAuditAirOrder = data => request('/Home/Audit/auditUpperOrder', 'POST', data)



// 获取店铺广告位
export const apiStorePosition = data => request('/Home/Storemanager/adImgList', 'POST', data)

// 活动订单列表
export const apiActivityOrderList = data => request('/Home/Audit/activityOrderList', 'POST', data)

// 活动订单详情
export const apiActivityOrderDetail = data => request('/Home/Audit/activityOrderDetail', 'POST', data)

// 订单审核
export const apiAuditOrder = data => request('/Home/Audit/auditOrder', 'POST', data)

// 删除活动 
export const apiDeleteActivity = data => request('/Home/Audit/delActivity', 'POST', data)

// 开放权限
export const apiOpenStore = data => request('/Home/Audit/open/auth', 'POST', data)

// 订单状态列表
export const apiOrderStatusList = data => request('/Home/Order/orderStatusList', 'POST', data)

// 上画/下画审核
export const apiOrderUpDownValid = data => request('/Home/Storemanager/auditWorkerRes', 'POST', data)

